package com.wjh.controller;

import com.wjh.bean.Constants;
import com.wjh.bean.Result;
import com.wjh.common.UrlCommon;
import com.wjh.feign.MemberServiceFeign;
import com.wjh.utils.CookiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/6/13.
 * @Modified By:
 */
@Controller
public class IndexController extends UrlCommon{

    @Autowired
    MemberServiceFeign memberServiceFeign;
    @GetMapping("/")
    public String index(HttpServletRequest request){
        Cookie cookie = CookiesUtil.getCookieByName(request, Constants.MEMBER_TOKEN_COOKIE);
        if (ObjectUtils.isEmpty(cookie)) {
            return "redirect:"+LOGIN;
//            return LOGIN;
        }
        String token = cookie.getValue();
        Result result = memberServiceFeign.findUser(token);
        if (result.getCode()!= Constants.HTTP_RES_CODE_200) {
            return "redirect:"+LOGIN;
        }
        LinkedHashMap linkedHashMap=(LinkedHashMap)result.getData();
        String username = (String)linkedHashMap.get("username");
        String updated = (String)linkedHashMap.get("updated");
//        String username = result.getData().getUsername();
        request.setAttribute("username",username);
        request.setAttribute("updated",updated);
        return INDEX;
    }
}
